#include <conio.h> //getch  kbhit
#include <windows.h> //sleep
#include <time.h> //clock

#include "Game.h"

//to edit the merged fill for the base map, go to Map::collisionDetection and change the
//three instances of '#', also go to Game::eraseRows and change the single instance of '#'

int main()
{
	//initialization phase
		//initialize game data
	Game g;
	int inputbuffer;
	bool printOnce = false;
		//generate any game data you need
		//load from files
	g.load();
		//etc...
	int waitTime = g.getFallRate();
	int lastTime = clock();
	//game loop
	while(!g.doesPlayerWantToQuit())
	{
		//if the keyboard was hit, handle input, otherwise draw
		if(kbhit())
		{
			//get input
			inputbuffer = getch();
			//handle input
			g.handleInput(inputbuffer);
		}
		//while not game over, update and draw
		else if(!g.getGameOver())
		{	
			//update the game by however long the game hasn't been updated
			g.update(clock()-lastTime); 
			lastTime = clock();
			//draw
			g.draw();
			//sets FPS 10
			//Sleep(100); //pauses for 100 milliseconds
			int timeWaitBegan = clock();
			//wait for the game to draw
			while(clock() < timeWaitBegan + waitTime && !kbhit())
			{
				Sleep(1);
				waitTime = g.getFallRate(); //update wait time incase fall rate has changed
			}
		}
		else if(!printOnce)
		{
			g.printGameOver();
			printOnce = true;//make sure the printGameOver() is only accessed once
							//otherwise the game over text will flicker
		}
	}
	//release phase
		//everything cleans up
		//deallocates
		//system resources are released (cleanly)
	g.release();
	return 0;
}
